package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@Api(tags = "文件上传")
@RequestMapping("/admin/common")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {

        //获取文件原始名
        String OriginalFilename = file.getOriginalFilename();
        //获得后缀
        String extName = OriginalFilename.substring(OriginalFilename.lastIndexOf("."));
        //重新起名，用uuid
        String filename= UUID.randomUUID().toString()+extName;
        //获得路径，调用aliossutil
        String filePath=aliOssUtil.upload(file.getBytes(),filename);
        return Result.success(filePath);
    }
}
